package com.yami.shop.platform.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.rights.common.model.ExchangeRecord;
import com.yami.shop.rights.common.service.ExchangeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class DeliveryExpireTask {

    @Autowired
    private ExchangeRecordService exchangeRecordService;
    @Autowired
    private ShopConfig shopConfig;

    /**
     * 兑换订单物流超期自动确认收货
     */
    @XxlJob("deliveryExpireTask")
    public void deliveryExpireTask() {
        String deliveryExpireMaxDay = shopConfig.getDeliveryExpireMaxDay();
        if(StrUtil.isNotEmpty(deliveryExpireMaxDay)){
            LambdaUpdateWrapper<ExchangeRecord> exchangeRecord1LambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            exchangeRecord1LambdaUpdateWrapper.eq(ExchangeRecord::getStatus,3);
            List<ExchangeRecord> list = exchangeRecordService.list(exchangeRecord1LambdaUpdateWrapper);
            for (ExchangeRecord exchangeRecord :list) {
                if(DateUtil.offsetDay(exchangeRecord.getUpdateTime(),Integer.valueOf(deliveryExpireMaxDay)).before(new Date()) ){
                    try {
                        exchangeRecordService.updateStatusByPersonRightsId(exchangeRecord.getOrderNumber());
                    }catch (Exception e){
                        log.error("超期自动确认收货"+e.getMessage());
                    }
                }
            }
        }
    }


}
